package com.swsc.xapp.xroom;

import android.text.TextUtils;

import com.swsc.xapp.utils.ConstantUils;

import java.util.Iterator;
import java.util.List;

public class InfraredImageService extends BaseService{
    private static volatile InfraredImageService instance;

    public static InfraredImageService getInstance(){
        if (instance == null) {
            synchronized (InfraredImageService.class) {
                if (instance == null) {
                    instance = new InfraredImageService();
                }
            }
        }
        return instance;
    }
    private InfraredImageService(){}

    public void deleteByTenantOrgan(Integer tenantNo, Integer organNo){
        db.infraredImageDao().deleteByTenantOrgan(tenantNo, organNo);
    }

    public void saveAll(List<InfraredImageDO> dos){
        db.infraredImageDao().saveCollection(dos);
    }

    public void save(InfraredImageDO infraredImageDo){
        db.infraredImageDao().save(infraredImageDo);
    }

    public List<InfraredImageDO> listByDefectNo(Long defectNo){
        return db.infraredImageDao().listByDefectNo(defectNo);
    }

    public List<InfraredImageDO> listByDefectNoIn(List<Long> defectNoIn){
        if(null != defectNoIn && defectNoIn.size() > 0){
//            StringBuilder sqlBuilder = new StringBuilder("select * from solar_infrared_image where 1 = 1");
            StringBuilder sqlBuilder = new StringBuilder("");
            sqlBuilder.append(" AND defect_no in (");
            for(int i = 0 ; i < defectNoIn.size(); i++){
                if(i > 0){
                    sqlBuilder.append(", ");
                }
                sqlBuilder.append(defectNoIn.get(i));
            }
            sqlBuilder.append(")");
            return db.infraredImageDao().doRawQuery(sqlBuilder.toString());
        }
        return null;
    }
    public List<InfraredImageDO> findByCondition(Integer tenantNo, Integer organNo, Long defectNo){
       StringBuilder sqlBuilder = parseBaseParamsBuilder(tenantNo, organNo);
       if(null != defectNo){
           sqlBuilder.append(" AND defect_no = ").append(defectNo);
       }
        return db.infraredImageDao().doRawQuery(sqlBuilder.toString());
    }

    public void deleteFaultImg(Long defectNo, String faultImgs, String fileBaseDir) {
        if(null != defectNo && !TextUtils.isEmpty(faultImgs)){
            List<InfraredImageDO> list = listByDefectNo(defectNo);
            if(null != list && list.size() > 0){
                String[] imgs = faultImgs.split(ConstantUils.SPILTOR_COMMA);
                if(null != imgs && imgs.length > 0){
                    Iterator<InfraredImageDO> iterator = list.iterator();
                    while (iterator.hasNext()){
                        InfraredImageDO infraredImage = iterator.next();
                        String imgUrl = infraredImage.getFileUrl();
                        if(!TextUtils.isEmpty(imgUrl)){
                            if(imgUrl.startsWith(fileBaseDir)){
                                imgUrl = imgUrl.replace(fileBaseDir, "");
                            }
                            for(String img : imgs){
                                // 图片拼接移除
                                if(img.startsWith(fileBaseDir)){
                                    img = img.replace(fileBaseDir, "");
                                }
                                if(img.equals(imgUrl)){
                                    iterator.remove();
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            if(null != (list)){
                for(InfraredImageDO infraredImage : list){
                    db.infraredImageDao().delete(infraredImage);
                }
            }
        }

    }
}
